package com.yuzhihao.writejvm.jnative;

import lombok.extern.log4j.Log4j2;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * java jdk
 *
 * @author yzh
 * @since 2021年12月22日11:51:57
 */
@Log4j2
public class Jnative {

    public static boolean isnative(Object obj){
        return Objects.isNull(obj.getClass().getClassLoader());
    }

    public static Object object(String object){
        object = object.replaceAll("/",".");
        try {
            Class<?> clazz = Class.forName(object);
            if(Objects.isNull(clazz.getClassLoader())){
                return clazz.newInstance();
            }else{
                // TODO: 2021/12/22 需要重新读取加载类信息，这个暂时不做处理
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Object field(Object object,String field){
        try {
            object.getClass().getField(field).get(object);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Object staticfield(String field){
        if("java/lang/System.out".equals(field)){
            return System.out;
        }
        if("java/lang/System.err".equals(field)){
            return System.err;
        }
        return null;
    }

    public static Object method(Object object,String method,Object args){
        try {
            Method method1 = object.getClass().getMethod(method,int.class);
            return method1.invoke(object,args);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        Class<?> aClass = Class.forName("java.lang.System");
        Class<?> zz = Class.forName("com.yuzhihao.writejvm.jnative.Jnative");

        Field field = aClass.getField("");


        System.out.println(aClass.getClassLoader());
        System.out.println(zz.getClassLoader());
    }

}
